package com.zjdiepu.www.rest.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Id;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.ParentModel;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.BusinessExtrasType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.request.ChangeChannelRequest;
import com.zjdiepu.www.model.yw.request.ComitInitialRequest;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 业务信息Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 19时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwBusinessController", produces="application/json;charset=UTF-8",method = RequestMethod.POST)
@Api(tags = {"业务信息"}, produces="application/json;charset=UTF-8")
public class YwBusinessController extends BaseController {
	@Resource
	private TokenUserHelper tokenUserHelper;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IDpBaseDealerInfoService dpBaseDealerInfoService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: changeChannel  <br/>
     * @Description: 修改订单类型 <br/>
     * @param changeChannelRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改订单类型", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("changeChannel")
    public ApiResponse changeChannel(@Validated(Update.class) @RequestBody ChangeChannelRequest changeChannelRequest) {
    	SysUser currentUser = tokenUserHelper.getUser();
    	ywBusinessService.saveChangeChannel(changeChannelRequest, currentUser);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getChannel  <br/>
     * @Description: 获取订单类型 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取订单类型", response = ChangeChannelRequest.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getChannel")
    public ApiResponse getChannel(@Validated(Id.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	String busiId = parentModel.getId();
    	
    	YwBusiness ywBusiness = ywBusinessService.selectById(busiId);
    	ChangeChannelRequest changeChannelRequest = new ChangeChannelRequest();
    	if(null == ywBusiness) {
    		ExceptionHelper.thow(ResultFlagType.MISSING.getCode().toString(), "业务记录不存在或已被删除");
    	}
    	ywBusinessVoMapper.mapper(ywBusiness);
    	changeChannelRequest.setId(busiId);
    	changeChannelRequest.setCanChange(10 == ywBusiness.getBusiStatus()); //只有资料登记状态可以修改
    	String channelType = ywBusiness.getChannelType();
    	changeChannelRequest.setStatus(StringUtils.isNotBlank(channelType)); //true:已登记
    	changeChannelRequest.setChannelType(channelType);
    	changeChannelRequest.setChannelTypeDisplay(ywBusiness.getChannelTypeDisplay());
    	Boolean ifPhone = null;
    	if("30".equals(channelType)) { /*两证贷*/
    		ifPhone = false;
    		//判断是否已经保存
			YwBusinessExtras ywBusinessExtras_ = new YwBusinessExtras();
			ywBusinessExtras_.setBusiId(busiId);
			ywBusinessExtras_.setTypeExtras(BusinessExtrasType.PHONE_AUDIT.getCode());
			ywBusinessExtras_.setCodeExtras(BusinessExtrasCodeType.IS_PHONE_AUDIT.getCode());
			ywBusinessExtras_.setDpStatus(DpStatus.NORMAL.getCode());
			ywBusinessExtras_ = ywBusinessExtrasService.selectRefined(ywBusinessExtras_);
			if(null != ywBusinessExtras_) {
				String valueExtras = ywBusinessExtras_.getValueExtras();
				ifPhone = StringUtils.isNotBlank(valueExtras) && "1".equals(valueExtras) ? true : false;
			}
    	}
    	changeChannelRequest.setIfPhone(ifPhone);
    	
    	String dealerId = ywBusiness.getDealerId();
    	changeChannelRequest.setDealerId(dealerId);
    	if(StringUtils.isNotBlank(dealerId)) {
    		DpBaseDealerInfo dpBaseDealerInfo = dpBaseDealerInfoService.selectById(dealerId);
    		if(null != dpBaseDealerInfo) {
    			String dealerName = dpBaseDealerInfo.getDealerName();
    			changeChannelRequest.setDealerName(dealerName);
    		}
    	}
    	
        return ApiResponse.success(changeChannelRequest);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: comitInitial  <br/>
     * @Description: 提交初审 <br/>
     * @param ywBusinessCarRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "提交初审", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("comitInitial")
    public ApiResponse comitInitial(@Validated(Update.class) @RequestBody ComitInitialRequest comitInitialRequest) {
    	SysUser currentUser = tokenUserHelper.getUser();
    	Boolean ifPhone = comitInitialRequest.getIfPhone();
        return ywBusinessService.saveCompleted(comitInitialRequest.getId(), currentUser, ifPhone == null ? false : ifPhone, comitInitialRequest.getChannelType());
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: comitFinal  <br/>
     * @Description: 提交终审 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "提交终审", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("comitFinal")
    public ApiResponse comitFinal(@Validated(Update.class) @RequestBody ParentModel parentModel,HttpServletRequest request) {
    	SysUser user = tokenUserHelper.getUser();
    	HttpSession session = request.getSession(true);      
		ServletContext  application  = session.getServletContext();  
		String serverRealPath = application.getRealPath("/WEB-INF/configure/fkshConfV001.json");
		ywBusinessService.saveComitFinal(parentModel.getId(), user, serverRealPath);
		return ApiResponse.success(true);
    }
}
